﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CorsatAdminApp.Library.TempTaskClasses
{
    public class Item
    {

        #region Private fields

        private int _itemId;

        private string _itemName;

        private string _itemDescription;

        private CommonItem _itemCategory;

        private CommonItem? _parentItem;

        #endregion

        #region Public properties

        public int ItemId
        {
            get { return _itemId; }
            set { _itemId = value; }
        }

        public string ItemName
        {
            get { return _itemName; }
            set { _itemName = value; }
        }

        public string ItemDescription
        {
            get { return _itemDescription; }
            set { _itemDescription = value; }
        }

        public CommonItem ItemCategory
        {
            get { return _itemCategory; }
            set { _itemCategory = value; }
        }

        public CommonItem? ParentItem
        {
            get { return _parentItem; }
            set { _parentItem = value; }
        }

        #endregion

        #region Constructors

        public Item()
        {
            ItemId = 0;
            ItemName = "";
            ItemDescription = "";
            ItemCategory = new CommonItem();
            ParentItem = null;
            
        }

        public Item(int itemId)
        {
            Item tempItem = new Item();
            //tempItem = DataManager.GetItem(itemId);
            this.ItemId = ItemId;
            this.ItemName = tempItem.ItemName;
            this.ItemDescription = tempItem.ItemDescription;
            this.ItemCategory = tempItem.ItemCategory;
            this.ParentItem = tempItem.ParentItem;
        }

        #endregion

        #region Private Methods

        #endregion

        #region Public Methods

        public Item GetItemById(int ItemId)
        {
            return null;
        }

        /// <summary>
        /// Saves the current Item
        /// </summary>
        /// <returns>true if successfully saved; false if not</returns>
        public bool Save()
        {
            try
            {
                if (ItemId == 0)
                {
                    // Insert new Item
                    return DataManager.Main.InsertItem(this);
                }
                else
                {
                    // Update Existing Item
                    return DataManager.Main.UpdateItem(this);
                }
                return true;
            }
            catch (Exception exp)
            {
                // Do Something with exp
                return false;
            }
        }

        /// <summary>
        /// Delete the current Item
        /// </summary>
        /// <returns>true if Successfuly Deleted; false if not</returns>
        public bool Delete()
        {

            try
            {
                // Delete the current Item
                return DataManager.Main.DeleteItem(this.ItemId);
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        public List<CommonItem> GetAllCategories()
        {
            try
            {
                return DataManager.Main.GetAllCategories();
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public List<Item> GetCategoryItems(int CategoryId)
        {
            try
            {
                return DataManager.Main.GetCategoryItems(CategoryId);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        #endregion

        #region OverrIdes

        public override string ToString()
        {
            return ItemName;
        }

        #endregion

    }

    // Header Item
    public struct CommonItem
    {

        public int ItemId { get; set; }
        public string ItemName { get; set; }
    }
}
